#coding:utf-8
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import os
import glob
import os.path
from tensorflow.python.platform import gfile

INPUT_DATA = 'E:/OK'
def distort_color(image,color_ordering=0):
    if color_ordering ==0:
        image = tf.image.random_brightness(image,max_delta=32./255.)
        image = tf.image.random_saturation(image,lower=0.5,upper=1.5)
        image = tf.image.random_hue(image,max_delta=0.2)
        image = tf.image.random_contrast(image,lower=0.5,upper=1.5)
    elif color_ordering ==1:
        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
        image = tf.image.random_brightness(image, max_delta=32. / 255.)
        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
        image = tf.image.random_hue(image, max_delta=0.2)
    else:
        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
        image = tf.image.random_hue(image, max_delta=0.2)
        image = tf.image.random_brightness(image, max_delta=32. / 255.)

    return tf.clip_by_value(image,0.0,1.0)
def preprocess_for_train(image,height,width,bbox,is_train = False):
    if bbox is None:
        bbox = tf.constant([0.0,0.0,1.0,1.0],dtype=tf.float32,shape=[1,1,4])
    #转换张量的类型
    if image.dtype != tf.float32:
        image = tf.image.convert_image_dtype(image,dtype=tf.float32)
    if is_train == False:
        Not_test_image = tf.image.resize_images(image,[height, width],method = 0)
        return Not_test_image
    #随机截图
    bbox_begin,bbox_size,_ = tf.image.sample_distorted_bounding_box(tf.shape(image),bounding_boxes=bbox)
    distort_image = tf.slice(image,bbox_begin,bbox_size)
    #将随机截取得图像调整为神经网络输入层的大小
    distort_image = tf.image.resize_images(distort_image,[height,width],method=np.random.randint(4))
    #随机左右翻转图像
    distort_image = tf.image.random_flip_left_right(distort_image)
    #使用随机一种顺序调整图像色彩
    distort_image = distort_color(distort_image,np.random.randint(3))
    return  distort_image


def get_image(INPUT_DATA):
    sub_dirs = [x[0] for x in os.walk(INPUT_DATA)]  # os.walk() 方法用于通过在目录树中游走输出在目录中的文件名，向上或者向下
    sub_dir = sub_dirs[np.random.randint(1,len(sub_dirs))]

    # 获取一个子目录中所有的图片文件
    # 由于windows系统下，文件名不区分大小写，所以应写作：
    extensions = ['jpg', 'jpeg']
    file_list = []
    dir_name = os.path.basename(sub_dir)  # 用到os.path.basename(),返回path最后的文件名。若path以/或\结尾，那么就会返回空值。
    for extension in extensions:
        file_glob = os.path.join(INPUT_DATA, dir_name, '*.' + extension)  # 将路径名整合到一起
        # extend将多个列表整合在一起，glob.glob函数匹配所有的符合条件的文件，并将其以list的形式返回,
        # 相当于通配符 daextensionsisy文件夹大约有633张JPG图片
        file_list.extend(glob.glob(file_glob))
    # 读取并解析图片，
    file_name = file_list[np.random.randint(len(file_list))]
    image_raw_data = gfile.FastGFile(file_name, 'rb').read()
    image_temp = tf.image.decode_jpeg(image_raw_data)
    return image_temp

#with tf.Session() as sess:
#     for i in range(2):
#         image_data = get_image(INPUT_DATA)
#         boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
#         result = preprocess_for_train(image_data,299,299,None)
#         print(image_data.shape[0])
#         plt.imshow(result.eval())
#         plt.show()

